﻿using MFTool;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MF_Base;
using MF_Base.Model;
using UIBase;
using UIBase.Helper;
using System.Web.Security;
using System.ComponentModel.Composition;

namespace PortalLTE.Controllers
{
    public class MainController : BaseController
    {
        public ActionResult Index()
        {
            ViewBag.AppTitle = ConfigurationManager.AppSettings["AppTitle"];
            ViewBag.TaskTimeSecSpan = ConfigurationManager.AppSettings["TaskTimeSecSpan"];
            ViewBag.CurrentUserName = GetCurrentUserName();
            return View();
        }
        public ActionResult IndexInner()
        {
            return View();
        }

        public ActionResult Home()
        {
            var sqlHelper = SqlHelper.CreateSqlHelper(EnumConn.MF_Base.ToString());
            string defineHomeColSql = @"select * from DefineHomeCol where DefineHomeId = 
                                     (select top 1 ID from DefineHome order by OrderIndex)";
            var defineHomeColTable = sqlHelper.ExcuteTable(defineHomeColSql);
            ViewBag.DefineHomeColTable = defineHomeColTable.ToDicList();
            return View();
        }

        public JsonResult GetModuleMenus(string id)
        {
            var funcList = GetFuncList();
            var children = funcList.Where(a => a.FullId.Contains(id) && a.Id != id).OrderBy(a => a.OrderIndex);
            return Json(EasyUIHelper.GetTreeJson(children), false);
        }

        public JsonResult GetModules()
        {
            var funcList = GetFuncList();
            var res = funcList.Where(a => a.EnumFuncType == EnumFuncType.Module.ToString()).OrderBy(a => a.OrderIndex);
            return Json(res,false);
        }

        public JsonResult GetMyToDoTaskCount()
        {
            var sqlHelper = SqlHelper.CreateSqlHelper(EnumConn.MF_WorkFlow.ToString());
            string sql = "select count(*) from WFStep where NextStepId is null and StepUserId like '%{0}%' ".ReplaceArg(GetCurrentUserID());
            var res = sqlHelper.ExecScalar(sql);
            int nRes = 0;
            if (res != null && res != DBNull.Value)
            {
                nRes = (int)res;
            }
            return Json(nRes);
        }

        public JsonResult LoginOut()
        {
            FormsAuthentication.SignOut();
            return Json("");
        }

        private List<MF_Func> GetFuncList()
        {
            var funcList = Session[CommonStr.SessionRoleFuncKey] as List<MF_Func>;
            if (funcList == null)
            {
                var sqlHelper = SqlHelper.CreateSqlHelper(EnumConn.MF_Base.ToString());
                var userRolesDt = sqlHelper.ExcuteTable("select * from MF_RoleUser where MF_UserId = '{0}'".ReplaceArg(GetCurrentUserID()));
                var userRoleIds = string.Join("','", userRolesDt.ToList().Select(a => a["MF_RoleId"].ToString()));
                var roleFuncsDt = sqlHelper.ExcuteTable(@"select distinct MF_Func.* from MF_FuncRole 
                                          inner join MF_Func on MF_Func.Id = MF_FuncRole.MF_FuncId where MF_FuncRole.MF_RoleId in ('{0}')".ReplaceArg(userRoleIds));
                funcList = roleFuncsDt.ToList<MF_Func>().ToList();
                Session[CommonStr.SessionRoleFuncKey] = funcList;
            }
            return funcList;
        }
    }
}
